import * as THREE from 'three'
import AnimationManager from './animationManager/index.ts'

class Engine {
	private readonly width: number
	private readonly height: number
	private scene: THREE.Scene | null = null
	private renderer: THREE.WebGLRenderer | null = null
	private camera: THREE.PerspectiveCamera | null = null
	private animationManager: AnimationManager

	constructor(dom: HTMLElement) {
		this.width = dom.clientWidth
		this.height = dom.clientHeight
		this.animationManager = new AnimationManager()
		this.initScene()
		this.initRenderer(dom)
		this.initCamera()
	}

	initScene() {
		this.scene = new THREE.Scene()
		this.scene.background = new THREE.Color(0xa0a0a0)
		this.scene.fog = new THREE.Fog(0xa0a0a0, 10, 50)

		const gridHelper = new THREE.GridHelper(150, 15)
		this.scene.add(gridHelper)
	}

	initRenderer(dom: HTMLElement) {
		this.renderer = new THREE.WebGLRenderer({ antialias: true })
		this.renderer.setPixelRatio(window.devicePixelRatio)
		this.renderer.setSize(this.width, this.height)
		dom.appendChild(this.renderer.domElement)
		this.animationManager.start()
	}

	initCamera() {
		this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 100)
		this.camera.position.set(1, 2, -3)
		this.camera.lookAt(0, 1, 0)
	}
}

export default Engine
